/**   
 * @Title: ThumLoader.java 
 * @Package com.culture.iptv.widget 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-6-17 ����3:48:18 
 * @version V1.0   
 */
package android.framework.tool;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Handler;
import android.provider.MediaStore.Images;

/**
 * @ClassName: ThumLoader
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-6-17 ����3:48:18
 * 
 */
public class ThumLoader {

	public interface OnThumListener {

		public void onThumLoad(String path,Bitmap bm);
		
		//public void onThumLoad(Integer t, Bitmap bm);

		//public void onError(Integer t);
		public void onError(String path);

	}

	private boolean firstLoad = true;
	private boolean mAllowLoad = true;
	private Object lock = new Object();
	private ExecutorService executorService = Executors.newFixedThreadPool(7);
	private ConcurrentHashMap<String, WeakReference> imageCache = new ConcurrentHashMap<String, WeakReference>();
	private Handler handler = new Handler();

	public void loadThum(final Integer mInteger, final String thumPath,
			final OnThumListener listener) {
		final OnThumListener mListener = listener;

		final String mThumPath = thumPath;

		final Integer mt = mInteger;

		executorService.execute(new Runnable() {

			@Override
			public void run() {

				if (!mAllowLoad) {

					synchronized (lock) {

						try {

							lock.wait();

						} catch (InterruptedException e) {
							e.printStackTrace();

						}

					}

				}

				if (mAllowLoad && firstLoad) {
					loadThums(mInteger, mThumPath, mListener);
				}
			}
		});
	}

	private void loadThums(final Integer mInteger, final String mThumPath,
			final OnThumListener mListener) {

		if (imageCache.containsKey(mThumPath)) {
			WeakReference softReference = imageCache.get(mThumPath);
			final Bitmap d = (Bitmap) softReference.get();
			if (d != null) {
				handler.post(new Runnable() {
					@Override
					public void run() {
						if (mAllowLoad) {
							mListener.onThumLoad(mThumPath, d);
						}
					}

				});
				return;

			}

		}
		try {
			final Bitmap bm = ThumbnailUtils.createVideoThumbnail(mThumPath,
					Images.Thumbnails.MINI_KIND);

			if (bm != null) {

				imageCache.put(mThumPath, new WeakReference(bm));

			}

			handler.post(new Runnable() {

				@Override
				public void run() {

					if (mAllowLoad) {

						mListener.onThumLoad(mThumPath, bm);

					}
				}

			});
		} catch (OutOfMemoryError e) {
			handler.post(new Runnable() {
				@Override
				public void run() {
					mListener.onError(mThumPath);
				}
			});
		} catch (Exception e) {
			handler.post(new Runnable() {
				@Override
				public void run() {
					mListener.onError(mThumPath);
				}
			});

		}
	}

}
